[T4Scaffolding.Scaffolder(Description = "Adds jqGrid partial view")][CmdletBinding()]
param(        
	[parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)][string]$Controller,
	[Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]$ModelType,
	[string]$ViewName = "_Index",
    [string]$Project,
	[string]$CodeLanguage,
	[string[]]$TemplateFolders,
	[switch]$Force = $false
)

$Controller = [System.Text.RegularExpressions.Regex]::Replace($Controller, "Controller$", "", [System.Text.RegularExpressions.RegexOptions]::IgnoreCase)
$foundControllerType = Get-ProjectType ($Controller + "Controller") -Project $Project
if (!$foundControllerType) { return }

$foundModelType = Get-ProjectType $ModelType -Project $Project

$foundRepositoryType = Get-ProjectType ($ModelType.ToLower() + "Repository") -Project $Project
if (!$foundRepositoryType) { return }

$primaryKey = Get-PrimaryKey $ModelType

$relatedEntities = [Array](Get-RelatedEntities $foundModelType.FullName -Project $project)
if (!$relatedEntities) { $relatedEntities = @() }

# Add the action method
$template = "jqGridAction"
Add-ClassMemberViaTemplate -CodeClass $foundControllerType -Template $template `
	-Model @{ 
	Repository = $ModelType.ToLower(); 
	PrimaryKey = [string]$primaryKey; 
	ViewDataType = [MarshalByRefObject]$foundModelType; 
	Test = [MarshalByRefObject]$foundRepositoryType; 
	RelatedEntities = $relatedEntities; } `
	-SuccessMessage "Added action method GridData to $($foundControllerType.Name)" `
	-TemplateFolders $TemplateFolders -Project $Project -CodeLanguage $CodeLanguage -Force:$Force

# Add the view
Scaffold MvcScaffolding.RazorView $Controller $ViewName -ModelType $ModelType `
	-Template jqGridView -Project $Project -CodeLanguage $CodeLanguage `
	-OverrideTemplateFolders $TemplateFolders -Force:$Force



